#!/usr/bin/env python
# -*- coding: utf-8 -*-

class filters:
	"""
	"""
	
	def __init__(self, s = None):
		self.string = ['']
		self.typ = "str"
		
		self.string, self.typ = self.setBuf(s, self.string)
		
	def setBuf(self, s1, s2):
		if str(type(s1)) == "<type 'str'>":
			s2[0] = s1
			t = "str"
		elif str(type(s1)) == "<type 'list'>":
			s2 = s1
			t = "list"
		else:
			print "WARRNING: in filters.__init__() \
-  Wrong parameter \" ", s1, " \""
			return [''], "str"
		return s2, t
		
	def func(self, f, s = None):
		buf, typ = [""], "str"
		if s:
			buf, typ = self.setBuf(s, buf)
		else:
			buf = self.string
			typ = self.typ
			
		result = map(lambda x: eval("x."+f), buf)
		
		if typ == "str":
			return result[0]
		else:
			return result
		
	def toUpper(self, s=None):
		return self.func("upper()", s)
	
	def toLower(self, s=None):
		return self.func("lower()", s)

	def toSwapcase(self, s=None):
		return self.func("swapcase()", s)
	
	def toTitle(self, s=None):
		return self.func("title()", s)
	
	def toCapitalize(self, s=None):
		return self.func("capitalize()", s)
	
	def setString(self, string):
		self.setBuf(string, self.string)
	
	def applyFilters(self, lst_filters):
		for f in lst_filters:
			try:
				self.string = eval("self." + f + "()")
			except AttributeError:
				print "No filter named: ", f
		
		return self.string
		
		
if __name__ == '__main__':
	s1 = filters("asdf")
	s2 = filters(["BNMN"])
	s3 = filters(2)
	print s1.typ, s2.typ, s1.string, s2.string
	
	print s1.toUpper()
	print s2.toLower()
	print s2.toUpper("qwe"), s2.toUpper(["fgh", "yuio"]), s2.string
	print s3.toSwapcase("adDwQWkP")
	print s3.toCapitalize("ahoj svete :-)")
	fls = ("toLower", "nonexistingfilter", "toCapitalize")
	s2.setString("UIOAISDjklkj")
	s2.applyFilters(fls)
	print s2.string
